"""
Case Type   : 功能测试
Case Name   : D库备份函数测试，执行成功
Create At   : 2025-09-19
Owner       : taoying
Description :
    1.创建D库并创建shark插件
    2.创建表格并执行object_id函数
    3.进行备份
    4.进行恢复
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
History     :
"""
import unittest
import time
import os
from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.ComThread import ComThread
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

class CommonuseFunction(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info('--Opengauss_Compatibility_Function_Commonuse_Case0049 start--')
        self.constant = Constant()
        self.common = Common()
        self.comsh = CommonSH('PrimaryDbUser')
        self.db_name = 'db_d'
        self.table_name = 'user_log'
        self.pri_node = Node('PrimaryDbUser')
        self.dump_path = os.path.join(macro.DB_BACKUP_PATH, 'FC_0049.sql')

    def testCommonuseFunction(self):
        self.log.info('------创建D库并创建shark插件------')
        create_db = f"drop database if exists {self.db_name};create database {self.db_name} dbcompatibility 'D';"
        res = self.comsh.execut_db_sql(create_db)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, res, '创建D库失败')
        create_ext = f"create extension if not exists shark;"
        res = self.comsh.execut_db_sql(create_ext, dbname=self.db_name)
        self.assertIn(self.constant.create_extension_success, res, '创建shark插件失败')

        text = '------step1:创建表格并执行object_id函数; expect:创建表格成功------'
        self.log.info(text)
        sql1 = f'''drop table if exists {self.table_name};
                   create table {self.table_name} (id SERIAL PRIMARY KEY,username VARCHAR(50),email VARCHAR(100),action_type VARCHAR(10),action_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
                   create or replace SYNONYM {self.table_name} FOR user1_log;
                   Select object_id('user1_log', 'SN');
                   Select object_id('public.user1_log');
                   select day('2025-02-27 23:59:59+08'::timestamptz) AS day_timestamptz;
                   select rand(1);'''
        session1 = ComThread(self.comsh.execut_db_sql, args=(sql1, '', self.db_name))
        session1.setDaemon(True)
        session1.start()
        session1.join(18)
        res1 = session1.get_result()
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, res1, '建表失败')
        self.assertTrue('object_id' in res1, 'object_id函数异常')
        self.assertTrue('rand' in res1, 'rand函数异常')

        text = '-----step2:gs_dump备份表; expect:成功-----'
        self.log.info(text)
        dump_res = self.comsh.exec_gs_dump(
            self.dump_path, f'-t {self.table_name}', get_detail=False, dbname=self.db_name)
        self.assertTrue(dump_res, f'执行失败:{text}')

        text = '-----step3:gsql恢复表; expect:成功-----'
        self.log.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gsql -d {self.pri_node.db_name} " \
              f"-p {self.pri_node.db_port} -f {self.dump_path}"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.pri_node, cmd)
        self.assertIn("SET", res, f'恢复数据失败:{text}')

    def tearDown(self):
        text = '------step4:清理环境;expect:成功------'
        self.log.info(text)
        del_sql = f"drop table if exists {self.table_name};"
        res = self.comsh.execut_db_sql(del_sql, dbname=self.db_name)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, res, '删表失败')

        drop_sql = f"drop database {self.db_name};"
        res = self.comsh.execut_db_sql(drop_sql)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, res, '删除D库失败')
        self.log.info(f'------Opengauss_Compatibility_Function_Commonuse_Case0049 end------')